package com.ruoyi.dl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dl.domain.DlActivity;
import com.ruoyi.dl.service.IDlActivityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动管理Controller
 * 
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/dl/activity")
public class DlActivityController extends BaseController
{
    private String prefix = "dl/activity";

    @Autowired
    private IDlActivityService dlActivityService;

    @RequiresPermissions("dl:activity:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/activity";
    }

    /**
     * 查询活动管理列表
     */
    @RequiresPermissions("dl:activity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlActivity dlActivity)
    {
        startPage();
        List<DlActivity> list = dlActivityService.selectDlActivityList(dlActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @RequiresPermissions("dl:activity:export")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlActivity dlActivity)
    {
        List<DlActivity> list = dlActivityService.selectDlActivityList(dlActivity);
        ExcelUtil<DlActivity> util = new ExcelUtil<DlActivity>(DlActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 新增活动管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存活动管理
     */
    @RequiresPermissions("dl:activity:add")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlActivity dlActivity)
    {
        //判断公告内容
        if (StringUtils.isEmpty(dlActivity.getContent())) {
            return error("活动内容不能为空！");
        }
        //判断活动开始和结束时间
        if (dlActivity.getEndTime().before(dlActivity.getStartTime())){
            return error("活动结束时间不能比开始时间早！");
        }
        return toAjax(dlActivityService.insertDlActivity(dlActivity));
    }

    /**
     * 修改活动管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DlActivity dlActivity = dlActivityService.selectDlActivityById(id);
        mmap.put("dlActivity", dlActivity);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动管理
     */
    @RequiresPermissions("dl:activity:edit")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlActivity dlActivity)
    {
        //判断公告内容
        if (StringUtils.isEmpty(dlActivity.getContent())) {
            return error("活动内容不能为空！");
        }
        //判断活动开始和结束时间
        if (dlActivity.getEndTime().before(dlActivity.getStartTime())){
            return error("活动结束时间不能比开始时间早！");
        }
        return toAjax(dlActivityService.updateDlActivity(dlActivity));
    }

    /**
     * 删除活动管理
     */
    @RequiresPermissions("dl:activity:remove")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlActivityService.deleteDlActivityByIds(ids));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = new HashMap<>();
        try {
            String avatar = FileUploadUtils.upload(file);
            map.put("data", avatar);
            map.put("code", "0");
        } catch (Exception e) {
            map.put("data", e.getMessage().toString());
            map.put("code", "0");
        }
        return map;
    }
}
